package cn.gp.chat.controller;

import cn.gp.chat.common.utils.ApiResponse;
import cn.gp.chat.commom.resp.MessageVo;
import cn.gp.chat.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    private final MessageService messageService; // final字段，手动实现构造函数注入，注解@Autoired，@Resource
    
    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    /**
     * @Param userId
     * @Param contactId
     * @Param limit
     * @Return cn.gp.chat.common.utils.ApiResponse<java.util.List < cn.gp.chat.commom.resp.MessageVo>>
     * @Description 获取历史消息记录 默认50条
     * @Author He Guoping
     * @Date 2025/6/19 9:50
     */
    @GetMapping("/history")
    public ApiResponse<List<MessageVo>> getChatHistory(
            @RequestParam Long userId,
            @RequestParam Long contactId,
            @RequestParam(required = false) Integer limit) {
        log.info("获取聊天历史: userId={}, contactId={}, limit={}", userId, contactId, limit);
        return messageService.getChatHistory(userId, contactId, limit);
    }

    /**
     * @Param id
     * @Return cn.gp.chat.common.utils.ApiResponse<cn.gp.chat.commom.resp.MessageVo>
     * @Description 获取单条消息记录
     * @Author He Guoping
     * @Date 2025/6/19 9:55
     */
    @GetMapping("/{id}")
    public ApiResponse<MessageVo> getMessage(@PathVariable Long id) {
        log.info("获取消息: id={}", id);
        MessageVo messageVo = messageService.getMessageById(id);
        if (messageVo == null) {
            return ApiResponse.fail(404, "消息不存在");
        }
        return ApiResponse.success(messageVo);
    }
} 